package com.winsomely.leecode;

/**
 * 多数元素
 * 
 * @author liujiawei
 *
 */
public class MajorityElement {

	public int majorityElement(int[] nums) {
		if (nums.length == 1) {
			return nums[0];
		}
		int count = 0;
		Integer num = null;
		for (int x : nums) {
			if (count == 0) {
				num = x;
			}
			count += num == x ? 1 : -1;
		}
		return num;
	}

	public static void main(String[] args) {
		MajorityElement t = new MajorityElement();
		int[] nums = { 2,2,1,1,1,2,2 };
		System.out.println(t.majorityElement(nums));
	}

}
